import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import {
    addMangEquipment, //保存
    getMangEquipmentByFid, //详情
} from "@/api/request.js";
export default {
    data() {
        return {
            visible: "",
            UseplaceS: "",
            loading: false,
            title: null,
            isLock: false,
            fFileurllist: [],
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: null, //主键id
                fProjectid: null, //建设项目id
                fProjectname: null, //建设项目简称
                fCompanyid: null, //施工标段id
                fCompanyname: null, //施工标段简称
                fUseplace: "", //外检设备类型
                fEqptno: null, //设备编号
                fEqptname: null, //设备名称
                fEqptspecs: null, //规格型号
                fFactoryname: null, //制造厂家
                fCertificationno: null, //生产编号
                fOutdates: this.common.day(0), //生产日期
                fOperuserid: null, //保管人id
                fOperusername: null, //保管人
                fSourcefile: null, //资料附件
                fNote: null, //备注
                userId: null, //当前登录用户id
                userName: null, //当前登录用户姓名
                recordfiles: [], //删除文件id集合--修改时传
            },
            formObj: formJs.formObj,
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selected: [],
            fCompanyids: null,
            primaryInfo :null,
        };
    },
    components: {
        publicForm,
        FormLyout
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.UseplaceS=this.primaryInfo.Useplace;
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;

            this.formData.fProjectname = data[0].projectName;
            this.formData.fProjectid = data[0].projectId;

            this.getTender(data[0].projectId);
          }, true);
        this.statusNum=this.primaryInfo.status;
        this.btnShow = true
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        if (this.statusNum == 1) { //新增
            // this.formData = this.$options.data.call(this).formData;
            this.formData.fUseplace = this.UseplaceS;
            this.timestamp = "EQUIP" + Date.parse(new Date());
            // this.formData.fProjectname = this.userInfo.projectName;
            // this.formData.fCompanyname = this.userInfo.companyName;
            this.formData.fSourcefile = this.timestamp;
            this.formData.fOperuserid = this.userInfo.id;
            this.formData.fOperusername = this.userInfo.userName;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[9].disabled = true;

            this.formData.fOutdates = this.common.day(0)
            this.formObj[10].uploadSelect = true
            this.btnShow = true

        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.primaryInfo.data.fId //按钮 
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[9].disabled = true;
            this.formObj[10].uploadSelect = true
            this.btnShow = true

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false;
          
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[10].uploadSelect = false
        }
      
    },
    methods: {
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: localStorage.getItem("access_token"),
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                     if (data.length == 1) {
                        this.formData.fCompanyname = data[0].sectionName;
                        this.formData.fCompanyid = data[0].sectionId;
                        this.formData.fItemvalue = data[0].sectionId
                    } 
                }
            });
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        selectChange(data, a, b) {
            console.log(data, a, b);

        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        

        // 详情
        getDetail(obj) {
            this.loading = true;
            getMangEquipmentByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.lrMangEquipment;
                    this.formData.recordfiles = [];
                    this.fFileurllist = res.data.fSourcefileList;
                    this.formData = data;
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm(status) {
            this.formData.userId = this.userInfo.id;
            this.formData.userName = this.userInfo.userName;

            this.formData.fItemvalue = this.fCompanyids
            // 0连续新增1新增
            // this.formData
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.isLock = true
            addMangEquipment(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$router.push({
                            path: "/reboundStrengthManage/reboundDataDictionary/reboundDetectionEquipment",

                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");
                        this.$message.success({
                            content:"新增成功",
                            duration: 1000
                        });
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);

                    }
                })


        },
    },
};